/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.inventory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.inventory.ItemHpp;
import model.inventory.ItemPrice;
import utility.NameValueList;
import utility.StringUtils;
import utility.com.TextComponentUtils;

/**
 *
 * @author jay
 */
public class ItemPriceTM extends AbstractTableModel{
    private List<ItemPrice> data;

    public ItemPriceTM() {
        this(new ArrayList<ItemPrice>(0));
    }

    public ItemPriceTM(List<ItemPrice> data) {
        this.data = data;
    }
     
    public ItemPrice get(int index) {
        return data.get(index);
    }

    public void add(ItemPrice product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, ItemPrice product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<ItemPrice> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }

    @Override
    public boolean isCellEditable(int row, int col) {
        return col<4 ? true:false;
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 5;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: return data.get(rowIndex).getId().getPriceLevel();
                
            case 1: 
                String displayMrg = TextComponentUtils.formatNumberShowDecimal(data.get(rowIndex).getMargin(), 2);
                return displayMrg; 
            case 2: 
                String displayMrk = TextComponentUtils.formatNumber(data.get(rowIndex).getMarkup());
                return displayMrk; 
            case 3: 
                String displayPrice = TextComponentUtils.formatNumber(data.get(rowIndex).getPrice());
                return displayPrice; 
            default: return null;
        }
    }
    
    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        switch (columnIndex) {
            
            case 0: 
                int angka = StringUtils.textToInt((String)value);
                data.get(rowIndex).getId().setPriceLevel(angka);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;
            
            case 1: 
                BigDecimal mrg = TextComponentUtils.parseNumberToBigDecimal((String)value);
                data.get(rowIndex).setMargin(mrg);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;
                
            case 2: 
                BigDecimal mrk = TextComponentUtils.parseNumberToBigDecimal((String)value);
                data.get(rowIndex).setMarkup(mrk);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;    
            
            case 3: 
                BigDecimal price = TextComponentUtils.parseNumberToBigDecimal((String)value);
                data.get(rowIndex).setPrice(price);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;        
        }        
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0: return "LEVEL";
            case 1: return "MARGIN %";
            case 2: return "MARKUP";            
            case 3: return "HARGA JUAL";
            default: return null;
        }
    }
}
